<template>
  <div class="messSty">
    <div>
      <el-input placeholder="请输入管理员用户名" v-model="adminUser.userName" @keyup.enter.native="adminlogin(adminUser)">
        <template slot="prepend">账号:</template>
      </el-input>
    </div>
    <br /><br />
    <div>
      <el-input placeholder="请输入密码" v-model="adminUser.passWord" @keyup.enter.native="adminlogin(adminUser)">
        <template slot="prepend">密码:</template>
      </el-input>
    </div>
    <br /><br />

    <el-container>
      <el-col :span="2"></el-col>
      <el-col :span="2"></el-col>
      <el-col :span="10"> </el-col> </el-container
    ><br /><br />

    <el-row>
      <el-button type="primary" @click="adminLogins(adminUser)" style="width: 100%"
        >登录</el-button
      >
    </el-row>
  </div>
</template>

<script>
import Cookie from "cookie_js";
export default {
  name: "adminLogin",
  data() {
    return {
      adminUser: {
        userName: "",
        passWord: "",
      },
    };
  },

  methods: {
    adminLogins(adminUser) {
      
      if (this.adminUser.userName != "" && this.adminUser.passWord != "") {
        
        this.$axios({
          method: "post",
          url: "/admin/AdminLogin",
          data: JSON.stringify(adminUser),
          headers: { "Content-Type": "application/json" },
        })
          .then((res) => {
            
            if (res.data.code == 200) {
              
              //userName和密码正确就往cookie里面存入token
              Cookie.set("token", res.data.data.token, { user: "localhost" });

              // 将管理员id存入本地
              localStorage.setItem("adminId", JSON.stringify(res.data.data.id));

              //路由跳转
              this.$router.push({
                name: "adminStudents",
              });
              
            }else{
				alert("用户名或密码错误")
			}
          })
          .catch((e) => {
            console.log("登录失败");
            console.log(e);
          });
      } else {
        alert("用户名或密码为空");
      }
    },
  },
};
</script>
